package gulimall.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;

/**
 * @author HLF
 * @date 2022/8/4 8:11
 */
@Configuration
public class SpringSessionConfig {

    /**
     * 设置Cookie信息
     */
    @Bean
    public CookieSerializer cookieSerializer() {
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        //设置父域
        cookieSerializer.setDomainName("firstgulimall.com");
        //设置Cookie的名字,类似于JSESSIONID
        cookieSerializer.setCookieName("GULIMALLSESSION");

        return cookieSerializer;
    }

    /**
     * 设置session存储在redis中为json格式
     */
    @Bean("springSessionDefaultRedisSerializer")
    public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
        return new Jackson2JsonRedisSerializer<>(Object.class);
    }
}
